﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Admin
{
    public partial class Withdraw : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btOK_Click(object sender, EventArgs e)
        {
            InsertsWithDraw();
            InsertsExpenditure();
            GV_ShowWithdraw.DataBind();
        }
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }

            return value;
        }
        public void InsertsWithDraw()
        {
            string sqltext = @"insert into [TB_WithDraw] 
                                   ( [WithDraw_date]
                                    ,[Date_WithDraw]
                                    ,[Employee_ID]
                                    ,[Num_WithDraw]
                                    ,[Signture_Admin])
                                   values (@Withdate,@Date,@Emp,@Num,@Signture)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            string[] datesplit = DateEdit_Withdraw.Value.ToString().Split(' ');
            string Time = DateTime.Now.ToString("HH:mm", new System.Globalization.CultureInfo("th-TH"));
            string DateT = datesplit[0] + " " + Time;
            Session["DateNow"] = Convert.ToDateTime(DateT).ToString("yyyy/MM/dd HH:mm", new System.Globalization.CultureInfo("en-US"));
            string Date = Convert.ToDateTime(DateEdit_Withdraw.Value).ToString("yyyy/MM/dd", new System.Globalization.CultureInfo("en-US"));
            sqlcomm.Parameters.AddWithValue("@Withdate", GetDataValue(Session["DateNow"].ToString()));
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(Date));
            sqlcomm.Parameters.AddWithValue("@Emp", GetDataValue(Cb_Emp.Value));
            sqlcomm.Parameters.AddWithValue("@Num", GetDataValue(tbNum.Text));
            sqlcomm.Parameters.AddWithValue("@Signture", GetDataValue(Session["User"].ToString()));//ส่ง session

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsExpenditure()
        {
            string sqltext = @"insert into [TB_Expenditure] 
                                   ( [Expenditure_Date]
                                    ,[Expenditure_List]
                                    ,[Expenditure_Price]
                                    ,[Referent])
                                   values (@Date,@List,@Price,@Referent)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@List", GetDataValue(Cb_Emp.Value+" เบิกจ่ายล่วงหน้า "));//ส่ง session
            sqlcomm.Parameters.AddWithValue("@Price", GetDataValue(tbNum.Text));
            sqlcomm.Parameters.AddWithValue("@Referent", GetDataValue(Session["DateNow"].ToString()));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        
    }
}